package com.lbb.mvc.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.Toast
import androidx.viewbinding.ViewBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.lbb.mvc.view.BaseView
import com.lbb.mvc.view.LayoutView
import com.lbb.mvc.view.ProgressDialogController
import com.lbb.mvc.view.StatusController
import com.lbb.utils.databinding.LayoutEmptyBinding
import com.lbb.utils.databinding.LayoutErrorBinding
import com.lbb.utils.databinding.LayoutLoadingBinding

abstract class BaseBottomSheetDialogFragment<B : ViewBinding> : BottomSheetDialogFragment(),
    BaseView, LayoutView<B> {
   lateinit var b: B
    private val progressDialog = ProgressDialogController()
    private val statusView = StatusController()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val frameLayout = FrameLayout(inflater.context)
        b = getLayoutContent()
        progressDialog.onAttach(activity)
        addStatusView(frameLayout)
        frameLayout.addView(b.root)
        return frameLayout
    }


    private fun addStatusView(frameLayout: FrameLayout) {
        if (isStatusView()) {
            val loading = getLayoutLoading()
            val empty = getLayoutEmpty()
            val error = getLayoutError()
            statusView.onAttach(b, loading, empty, error)
            val params = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            frameLayout.addView(loading?.root, params)
            frameLayout.addView(empty?.root, params)
            frameLayout.addView(error?.root, params)
        }
        showContentView()
    }

    override fun showToast(msg: String) {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
    }

    override fun showTopProgressDialog(s: String?, cancelable: Boolean?) {
        progressDialog.showTopProgressDialog(s, cancelable)
    }

    override fun showCenterProgressDialog(s: String?, cancelable: Boolean?) {
        progressDialog.showCenterProgressDialog(s, cancelable)
    }

    override fun dismissTopProgressDialog() {
        progressDialog.dismissTopProgressDialog()
    }

    override fun dismissCenterProgressDialog() {
        progressDialog.dismissCenterProgressDialog()
    }

    override fun dismissProgressDialog() {
        progressDialog.dismissProgressDialog()
    }

    override fun isStatusView(): Boolean {
        return statusView.isStatusView()
    }


    override fun showContentView() {
        statusView.showContentView()
    }

    override fun showLoadingView() {
        statusView.showLoadingView()
    }

    override fun showEmptyView() {
        statusView.showEmptyView()
    }

    override fun showErrorView(listener: View.OnClickListener?) {
        statusView.showErrorView(listener)
    }


    override fun getLayoutLoading(): ViewBinding? {
        return LayoutLoadingBinding.inflate(layoutInflater)
    }

    override fun getLayoutEmpty(): ViewBinding? {
        return LayoutEmptyBinding.inflate(layoutInflater)
    }

    override fun getLayoutError(): ViewBinding? {
        return LayoutErrorBinding.inflate(layoutInflater)
    }
}